package demo1;

import java.util.Arrays;
import java.util.HashMap;
import java.util.Map;

/**
 * @Author liangzai
 * @Description:
 */
public class Training {
    public int waysToSplitArray(int[] nums) {
        long sum = 0;
        for(int num : nums){
            sum += num;
        }
        long preSum = 0;
        int count = 0;
        for(int i = 0;i < nums.length - 1;i++){
            int num = nums[i];
            preSum += num;
            // 前i+1个数大于等于后面(n-i-1)个数
            if(preSum >= sum - preSum){
                count++;
            }
        }
        return count;
    }

    public int bestTeamScore(int[] scores, int[] ages) {
        int n = scores.length;
        int[][] ans = new int[n][];
        for (int i = 0; i < n; i++) {
            ans[i] = new int[]{ages[i], scores[i]};
        }
        Arrays.sort(ans, (o1, o2) -> {
            if (o1[0] == o2[0]) {
                return o1[1] > o2[1] ? 1 : -1;
            }
            return o1[0] > o2[0] ? 1 : -1;
        });
        scores[0] = ans[0][1];
        int MAX = scores[0];
        for (int i = 1; i < n; i++) {
            int max = ans[i][1];
            for (int j = 0; j < i; j++) {
                if (ans[i][1] >= ans[j][1]) {
                    max = Math.max(scores[j] + ans[i][1], max);
                }
            }
            scores[i] = max;
            MAX = Math.max(max, MAX);
        }
        return MAX;
    }

    public int similarPairs(String[] words) {
        int ans = 0;
        HashMap<String, Integer> map = new HashMap<>();
        for (int i = 0; i < words.length; i++) {
            int[] arr = new int[26];
            for (int k = 0; k < words[i].length(); k++) {
                arr[words[i].charAt(k) - 'a'] = 1;
            }
            StringBuilder sb = new StringBuilder();
            for (int m = 0; m < 26; m++) {
                if (arr[m] == 1) {
                    sb.append((char) (m + 'a'));
                }
            }
            map.put(sb.toString(), map.getOrDefault(sb.toString(), 0) + 1);
        }
        for (Map.Entry<String, Integer> entry : map.entrySet()) {
            int value = entry.getValue();
            ans += (value * (value - 1) / 2);
        }
        return ans;
    }
}
